﻿// Learn more about F# at http://fsharp.net
#light

open System
open System.Windows
open System.Windows.Media.Imaging
open Microsoft.Kinect
open System.Diagnostics

let sensor = KinectSensor.KinectSensors.[0]

//The main canvas that is handling the ellipses
let canvas = new System.Windows.Controls.Canvas()
canvas.Background <- System.Windows.Media.Brushes.Transparent

let ds : byte = Convert.ToByte(1)
let dummySkeleton : Skeleton = new Skeleton(TrackingState = SkeletonTrackingState.Tracked)

// Thanks to Richard Minerich (@rickasaurus) for helping me figure out
// some array concepts in F#.
let mutable pixelData : byte array = [| |]
let mutable skeletons : Skeleton array = [| |]

//Right hand ellipse
let rhEllipse = new System.Windows.Shapes.Ellipse()
rhEllipse.Height <- 20.0
rhEllipse.Width <- 20.0
rhEllipse.Fill <- System.Windows.Media.Brushes.Red
rhEllipse.Stroke <- System.Windows.Media.Brushes.White

//Left hand ellipse
let lhEllipse = new System.Windows.Shapes.Ellipse()
lhEllipse.Height <- 20.0
lhEllipse.Width <- 20.0
lhEllipse.Fill <- System.Windows.Media.Brushes.Red
lhEllipse.Stroke <- System.Windows.Media.Brushes.White

//Head ellipse
let hEllipse = new System.Windows.Shapes.Ellipse()
hEllipse.Height <- 20.0
hEllipse.Width <- 20.0
hEllipse.Fill <- System.Windows.Media.Brushes.Red
hEllipse.Stroke <- System.Windows.Media.Brushes.White

canvas.Children.Add(rhEllipse) |> ignore
canvas.Children.Add(lhEllipse) |> ignore
canvas.Children.Add(hEllipse) |> ignore

let grid = new System.Windows.Controls.Grid()

let winImage = new System.Windows.Controls.Image()
winImage.Height <- 600.0
winImage.Width <- 800.0

grid.Children.Add(winImage) |> ignore
grid.Children.Add(canvas) |> ignore

//Video frame is ready to be processed.
let VideoFrameReady (sender : obj) (args: ColorImageFrameReadyEventArgs) = 
    let receivedData = ref false

    using (args.OpenColorImageFrame()) (fun r -> 
        if (r <> null) then
            (
                pixelData <- Array.create r.PixelDataLength ds
                //Array.Resize(ref pixelData, r.PixelDataLength)
                r.CopyPixelDataTo(pixelData)
                receivedData := true
        )
        
        if (receivedData <> ref false) then
            (
            winImage.Source <- BitmapSource.Create(640, 480, 96.0, 96.0, Media.PixelFormats.Bgr32, null, pixelData, 640 * 4)
        )
        )

//Required to correlate the skeleton data to the PC screen
//IMPORTANT NOTE: Code for vector scaling was imported from the Coding4Fun Kinect Toolkit
//available here: http://c4fkinect.codeplex.com/
//I only used this part to avoid adding an extra reference.
let ScaleVector (length : float32, position : float32)  =
    let value = (((length / 1.0f) / 2.0f) * position) + (length / 2.0f)
    if value > length then
        length
    elif value < 0.0f then
        0.0f
    else
        value

//This will set the ellipse positions depending on the passed instance and joint
let SetEllipsePosition (ellipse : System.Windows.Shapes.Ellipse, joint : Joint) =
    let vector = new Microsoft.Kinect.SkeletonPoint(X = ScaleVector(640.0f, joint.Position.X), Y=ScaleVector(480.0f, -joint.Position.Y),Z=joint.Position.Z)
    let mutable uJoint = joint
    uJoint.TrackingState <- JointTrackingState.Tracked
    uJoint.Position <- vector

    System.Windows.Controls.Canvas.SetLeft(ellipse,(float uJoint.Position.X))
    System.Windows.Controls.Canvas.SetTop(ellipse,(float uJoint.Position.Y))

//Triggered when a new skeleton frame is ready for processing
let SkeletonFrameReady (sender : obj) (args: SkeletonFrameReadyEventArgs) = 
    let receivedData = ref false
    
    using (args.OpenSkeletonFrame()) (fun r -> 
        if (r <> null) then
            (
                skeletons <- Array.create r.SkeletonArrayLength dummySkeleton
                r.CopySkeletonDataTo(skeletons)

                for i in skeletons do
                    Debug.WriteLine(i.TrackingState.ToString())

                receivedData := true
        )
        
        if (receivedData <> ref false) then
            (
                for i in skeletons do
                    if i.TrackingState <> SkeletonTrackingState.NotTracked then
                        (
                        let currentSkeleton = i

                        SetEllipsePosition(hEllipse, currentSkeleton.Joints.[JointType.Head])
                        SetEllipsePosition(lhEllipse, currentSkeleton.Joints.[JointType.HandLeft])
                        SetEllipsePosition(rhEllipse, currentSkeleton.Joints.[JointType.HandRight])
                   )
        )
        )


let WindowLoaded (sender : obj) (args: RoutedEventArgs) = 
    sensor.Start()
    sensor.ColorStream.Enable()
    sensor.SkeletonStream.Enable()
    sensor.ColorFrameReady.AddHandler(new EventHandler<ColorImageFrameReadyEventArgs>(VideoFrameReady))
    sensor.SkeletonFrameReady.AddHandler(new EventHandler<SkeletonFrameReadyEventArgs>(SkeletonFrameReady))
 
let WindowUnloaded (sender : obj) (args: RoutedEventArgs) = 
    sensor.Stop()

//Defining the structure of the test window
let window = new Window()
window.Width <- 800.0
window.Height <- 600.0
window.Title <- "Kinect Skeleton Application"
window.Loaded.AddHandler(new RoutedEventHandler(WindowLoaded))
window.Unloaded.AddHandler(new RoutedEventHandler(WindowUnloaded))
window.Content <- grid
window.Show()

[<STAThread()>]
do 
    let app = new Application() in
    app.Run(window) |> ignore

